package com.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.entity.Admin;
import com.entity.Master;
import com.entity.Student;
import com.github.pagehelper.PageInfo;
import com.service.AdminService;
import com.service.MasterService;
import com.service.StudentService;
import com.util.MD5Util;

@Controller

public class AdminController {
	@Autowired
	private AdminService as;
	@Autowired
	private MasterService ms;
	@Autowired
	private StudentService ss;

	// 管理员登录
	@PostMapping(value = "/adminlogin", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject adminLogin(@RequestBody Admin admin, HttpSession session) {
		System.out.println(admin.getAid());
		System.out.println("加密前：" + admin.getPassword());
		JSONObject json = new JSONObject();
		int status = -1;
		String msg = "登陆失败";
		Admin adminLogin = as.adminLogin(admin.getAid(), MD5Util.encode(admin.getPassword()));
		System.out.println(admin.getAid());
		System.out.println("加密后：" + MD5Util.encode(admin.getPassword()));
		try {
			if ("".equals(admin.getAid()) || "".equals(admin.getPassword())) {
				status = -1;
				msg = "账号或密码不能为空";
				json.put("status", status);
				json.put("msg", msg);
				return json;
			} else {
				if (adminLogin != null) {
					status = 0;
					msg = "登录成功";
					session.setAttribute("admin", admin);
					json.put("status", status);
					json.put("msg", msg);
					return json;
				} else {
					json.put("status", -1);
					json.put("msg", "用户不存在");
					return json;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("status", -1);
			json.put("msg", "操作失败，请稍后再试");
			return json;
		}

	}

	// 修改密码
	@PostMapping(value = "/updateadminpassword", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject updateAdminpassword(HttpSession session, @RequestBody Admin admin) {
		System.out.println(admin.getAid());
		System.out.println("加密前：" + admin.getPassword());
		JSONObject json = new JSONObject();
		int status = -1;
		String msg = "修改失败";
		boolean updateAdminpassword = as.updateAdminpassword(admin.getAid(), MD5Util.encode(admin.getPassword()));
		System.out.println(admin.getAid());
		System.out.println("加密后：" + MD5Util.encode(admin.getPassword()));
		if ("".equals(admin.getPassword())) {
			status = 1;
			msg = "密码不能为空";
			json.put("status", status);
			json.put("msg", msg);
			return json;
		} else if (updateAdminpassword) {
			status = 0;
			msg = "修改成功";
			json.put("status", status);
			json.put("msg", msg);
			return json;
		}
		json.put("status", status);
		json.put("msg", msg);
		return json;

	}

	// 管理员退出登录
	@PostMapping(value = "/exitadmin", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject exitAdmin(HttpSession session) {
		JSONObject json = new JSONObject();
		int status = -1;
		String msg = "操作失败，请稍后再试";
		try {
			// 销毁session
			session.invalidate();
			status = 0;
			msg = "退出成功";

		} catch (Exception e) {
			e.printStackTrace();
		}
		json.put("status", status);
		json.put("msg", msg);
		return json;

	}

	// 查看个人信息
	@PostMapping(value = "/findadmininfobyid", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject findAdminInfoById(@RequestBody Admin admin) {
		JSONObject json = new JSONObject();
		int status = 0;
		String msg = "操作成功";
		Admin result = null;
		try {
			result = as.findAdminInfoById(admin.getAid());
		} catch (Exception e) {
			e.printStackTrace();
			status = -1;
			msg = "操作失败，请稍后再试";
		}
		json.put("result", result);
		json.put("status", status);
		json.put("msg", msg);
		return json;

	}

	// 修改管理员信息
	@PostMapping(value = "/updateadmininfo", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject updateAdminInfo(@RequestBody Admin admin, HttpSession session) {
		System.out.println(admin);
		JSONObject json = new JSONObject();
		int status = -1;
		String msg = "";
		// Admin admin2 = (Admin) session.getAttribute("admin");
		Admin fAdmin = null;
		try {
			if (as.findAdminInfoById(admin.getAid()) != null) {
				as.updateAdminInfo(admin);
				session.setAttribute("admin", admin);
				status = 0;
				msg = "修改成功";
			} else {
				status = -1;
				msg = "修改失败";
			}
			fAdmin = as.findAdminInfoById(admin.getAid());
		} catch (Exception e) {
			e.printStackTrace();
			status = -1;
			msg = "操作失败，请稍后再试";
		}
		json.put("fAdmin", fAdmin);
		json.put("status", status);
		json.put("msg", msg);
		return json;

	}

	// 分页查询舍管
	@PostMapping(value = "/findallmasters", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Map<String, Object> findAllMasters(
			@RequestParam(value = "pageNum", required = true, defaultValue = "1") int pageNum,
			@RequestParam(value = "pageSize", required = true, defaultValue = "5") int pageSize) {
		Map<String, Object> map = new HashMap<>();
		// 从第一页开始，每页5条
		List<Master> list1 = ms.findAllMasters(pageNum, pageSize);
		// 将信息放到pageinfo中去
		PageInfo<Master> pageinfo = new PageInfo<Master>(list1);
		map.put("status", 0);
		map.put("msg", "分页信息");
		map.put("pageinfo", pageinfo);
		return map;

	}

	// 根据宿舍楼查询舍管（分页）
	@PostMapping(value = "/findmastersbydormitory", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Map<String, Object> findMastersByDormitory(@RequestBody Master master,
			@RequestParam(value = "pageNum", required = true, defaultValue = "1") int pageNum,
			@RequestParam(value = "pageSize", required = true, defaultValue = "5") int pageSize) {
		Map<String, Object> map = new HashMap<>();
		// 从第一页开始，每页5条
		List<Master> list2 = ms.findMastersByDormitory(master.getDormitory(), pageNum, pageSize);
		// 将信息放到pageinfo中去
		PageInfo<Master> pageinfo = new PageInfo<Master>(list2);
		map.put("status", 0);
		map.put("msg", "分页信息");
		map.put("pageinfo", pageinfo);
		return map;
	}

	// 添加舍管
	@PostMapping(value = "/addmaster", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject addMaster(
			@RequestBody Master master/*
										 * , String mname, String password,
										 * String sex, String phone, String
										 * school, String dormitory
										 */) {
		JSONObject json = new JSONObject();
		// int status = -1;
		// String msg = "操作失败";
		if (master.getMname() == null || master.getMname().equals("")) {
			json.put("status", -1);
			json.put("msg", "名字不能为空");
			return json;
		} else if (master.getPassword() == null || master.getPassword().equals("")) {
			json.put("status", -1);
			json.put("msg", "密码不能为空");
			return json;
		} else if (master.getSex() == null || master.getSex().equals("")) {
			json.put("status", -1);
			json.put("msg", "性别不能为空");
			return json;
		} else if (master.getPhone() == null || master.getPhone().equals("")) {
			json.put("status", -1);
			json.put("msg", "电话不能为空");
			return json;
		} else if (master.getPhone() == null || master.getPhone().equals("")) {
			json.put("status", -1);
			json.put("msg", "学校不能为空");
			return json;
		} else if (master.getDormitory() == null || master.getDormitory().equals("")) {
			json.put("status", -1);
			json.put("msg", "宿舍楼不能为空");
			return json;
		}
		try {
			if (ms.findMasterByPhone(master.getPhone())) {
				json.put("status", -1);
				json.put("msg", "该用户已存在，请重试");
				return json;
			}
			ms.addMaster(master);
			json.put("status", 0);
			json.put("msg", "添加成功");
			return json;
		} catch (Exception e) {
			e.printStackTrace();
			json.put("status", -1);
			json.put("msg", "操作失败，请稍后再试");
			return json;
		}

	}

	// 修改舍管信息
	@PostMapping(value = "/updatemasterinfo", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject updateMasterInfo(@RequestBody Master master, HttpSession session) {
		System.out.println(master);
		JSONObject json = new JSONObject();
		int status = -1;
		String msg = "修改失败";
		Master fMaster = null;
		try {
			if (ms.isexistMaster(master.getMid()) != null) {
				ms.updateMaster(master);
				status = 0;
				msg = "修改成功";
			} else {
				status = -1;
				msg = "修改失败";
			}
			fMaster = ms.isexistMaster(master.getMid());
		} catch (Exception e) {
			e.printStackTrace();
			status = -1;
			msg = "操作失败，请稍后再试";
		}
		json.put("fMaster", fMaster);
		json.put("status", status);
		json.put("msg", msg);
		return json;

	}

	// 删除舍管
	@PostMapping(value = "/deletemaster", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject deleteMasterById(@RequestBody Master master) {
		JSONObject json = new JSONObject();
		int status = 0;
		String msg = "删除成功";
		boolean result = false;
		try {
			result = ms.deleteMasterById(master.getMid());
		} catch (Exception e) {
			e.printStackTrace();
			json.put("status", -1);
			json.put("msg", "操作失败，请稍后再试");
			return json;
		}
		if (!result) {
			status = -1;
			msg = "用户不存在";
		} else {
			status = 0;
			msg = "删除成功";
		}
		json.put("status", status);
		json.put("msg", msg);
		return json;

	}

	// 管理员查看所有学生（分页）
	@PostMapping(value = "/findallstudents", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Map<String, Object> findAllStudents(
			@RequestParam(value = "pageNum", required = true, defaultValue = "1") int pageNum,
			@RequestParam(value = "pageSize", required = true, defaultValue = "5") int pageSize) {
		Map<String, Object> map = new HashMap<>();
		// 从第一页开始，每页5条
		List<Student> list3 = ss.findAllStudents(pageNum, pageSize);
		// 将信息放到pageinfo中去
		PageInfo<Student> pageinfo = new PageInfo<Student>(list3);
		map.put("status", 0);
		map.put("msg", "分页信息");
		map.put("pageinfo", pageinfo);
		return map;
	}

	// 管理员多条件查询学生（分页）
	@PostMapping(value = "/findstudentbymultiple", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Map<String, Object> findStudentByMultiple(@RequestBody Student student,
			@RequestParam(value = "pageNum", required = true, defaultValue = "1") int pageNum,
			@RequestParam(value = "pageSize", required = true, defaultValue = "5") int pageSize) {
		Map<String, Object> map = new HashMap<>();
		// 从第一页开始，每页5条
		List<Student> list4 = ss.findStudentByMultiple(student, pageNum, pageSize);
		// 将信息放到pageinfo中去
		PageInfo<Student> pageinfo = new PageInfo<Student>(list4);
		map.put("status", 0);
		map.put("msg", "分页信息");
		map.put("pageinfo", pageinfo);
		return map;
	}

}
